Use SmallVec in NeighborTable for cache locality#10784
Merged
Merged
Conversation
A reasonable chunk of our time in Sabre is spent reading through the `NeighborTable` to find the candidate swaps for a given layout. Most coupling maps that we care about have a relatively low number of edges between qubits, yet we needed to redirect to the heap for each individual physical-qubit lookup currently. This switches from using a `Vec` (which is always a fat pointer to heap memory) to `SmallVec` with an inline buffer space of four qubits. With the qubit type being `u32`, the `SmallVec` now takes up the same stack size as a `Vec` but can store (usually) all the swaps directly inline in the outer `Vec` of qubits. This means that most lookups of the available swaps are looking in the same (up to relatively small offsets) in memory, which makes the access patterns much easier for prefetching to optimise for.
Collaborator
|
One or more of the the following people are requested to review this:
|
mtreinish
previously approved these changes
Sep 6, 2023
Member
mtreinish
left a comment
There was a problem hiding this comment.
LGTM this is a very straighforward change and moving the data structure to small vec makes a lot of sense. I have one small question inline but it's non blocking more just curiosity than anything to change.
`SmallVec` doesn't have implementations of the PyO3 conversion trait, so it needs to be done manually. The previous state used to convert to a Rust-space `Vec` that then needed to have its data moved from the Python heap to the Rust heap. This instead changes the conversions to interact directly with Python lists, rather than using intermediary structures.
mtreinish
approved these changes
Sep 6, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A reasonable chunk of our time in Sabre is spent reading through the
NeighborTableto find the candidate swaps for a given layout. Most coupling maps that we care about have a relatively low number of edges between qubits, yet we needed to redirect to the heap for each individual physical-qubit lookup currently.This switches from using a
Vec(which is always a fat pointer to heap memory) toSmallVecwith an inline buffer space of four qubits. With the qubit type beingu32, theSmallVecnow takes up the same stack size as aVecbut can store (usually) all the swaps directly inline in the outerVecof qubits. This means that most lookups of the available swaps are looking in the same (up to relatively small offsets) in memory, which makes the access patterns much easier for prefetching to optimise for.Details and comments
This is a less pronounced effect than either #10782 or #10783, but is still a measurable performance improvement - the example in #10782 went from 2.08(4)s on
mainto 1.96(3)s with this PR (again - the numbers are bigger because Webex is using some of my CPU cycles haha).